package com.cty.service.impl;


import com.cty.mapper.PraiseMapper;
import com.cty.pojo.Praise;
import com.cty.pojo.ShopReview;
import com.cty.service.PraiseService;
import com.cty.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PraiseServiceImpl implements PraiseService {
    @Autowired
    private PraiseMapper praiseMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ReviewService reviewService;

    /**
     * redis 中的点赞数据 持久化到mysql中
     * (1) 用户点赞记录 批量插入到 mysql   -- insert batch
     * (2) 修改mysql中 reivew点评表中的点赞数 -- update
     */
    @Scheduled(cron = "0/15 * * * * ?")
    @Override
    public void savePraiseFromRedisDataSave2DB() {
        //1. 获得redis 中的用户点赞记录 ， 保存到mysql
        Map<String,Integer> custLikeRecordMap =redisTemplate.opsForHash().entries("save_praise_record");
        System.out.println(custLikeRecordMap);
        Set<Map.Entry<String,Integer>> set = custLikeRecordMap.entrySet();

        List<Praise> praiseList = new ArrayList<>();
        for(Map.Entry<String,Integer> entry:set){ //10001::50035=1,
            Integer customerId =  Integer.valueOf( entry.getKey().split("::")[0]); //10001
            Integer reviewId =  Integer.valueOf( entry.getKey().split("::")[1]);  //50035
            Integer praiseStatus = entry.getValue(); //1
            Praise praise = new Praise();
            praise.setCusId(customerId);
            praise.setRecReviewId(reviewId);
            praise.setPraiseStatus(praiseStatus);
            praise.setPraiseTime(new Date());
            System.out.println("点评记录中praise:"+praise);
            // 不能将每一个Praise对象 逐条添加到 mysql， 原因是redis中数据量很大，会出现高并发的对mysql数据库操作
            //这样的话，redis 存在的意义就没有了， 解决方案是  批量添加
            praiseList.add(praise);
        }


        //批量添加,insert into praise value(...),(....) 效率高，不是单条添加
        if(praiseList.size()>0) {
            System.out.println("praiseList:" + praiseList);
            saveAll(praiseList);
        }

        // 从redis 中删除
       if(redisTemplate.hasKey("save_praise_record")) {
            boolean result = redisTemplate.delete("save_praise_record");
            System.out.println("从redis中移除 customer_liked_record 的结果是：" + result);
        }


        //2. 获得redis 中 评论点赞总数，修改到mysql中
        Map<String,Integer> reviewPraiseCountMap =redisTemplate.opsForHash().entries("review_praise_count");
        System.out.println(reviewPraiseCountMap); //50035=4,

        Set<Map.Entry<String,Integer>> set2 = reviewPraiseCountMap.entrySet();
        for(Map.Entry<String,Integer> entry:set2) { //
            System.out.println("entry.getKey():"+entry.getKey());
            System.out.println("entry.getValue():"+entry.getValue());
            Integer reviewId =   Integer.valueOf(entry.getKey()  ); //50035
            Integer praiseCount =   entry.getValue() ;   // 4
            System.out.println("reviewId:" + reviewId);
            System.out.println("praiseCount:" + praiseCount);

            ShopReview review = new ShopReview();
            review.setShopReviewId(reviewId);
            review.setReviewPraise(praiseCount);

            reviewService.update(review);

        }




    }


    @Scheduled(cron = "0/15 * * * * ?")
    @Override
    public void canclePraiseFromRedisDataSave2DB() {
        //1. 获得redis 中的用户点赞记录 ， 保存到mysql
        Map<String,Integer> custLikeRecordMap =redisTemplate.opsForHash().entries("cancle_praise_record");
        System.out.println(custLikeRecordMap);
        Set<Map.Entry<String,Integer>> set = custLikeRecordMap.entrySet();

        List<Praise> praiseList = new ArrayList<>();
        for(Map.Entry<String,Integer> entry:set){ //10001::50035=1,
            Integer customerId =  Integer.valueOf( entry.getKey().split("::")[0]); //10001
            Integer reviewId =  Integer.valueOf( entry.getKey().split("::")[1]);  //50035
            Integer praiseStatus = entry.getValue(); //1
            Praise praise = new Praise();
            praise.setCusId(customerId);
            praise.setRecReviewId(reviewId);
            praise.setPraiseStatus(praiseStatus);
            praise.setPraiseTime(new Date());
            System.out.println("点评记录中praise:"+praise);
            // 不能将每一个Praise对象 逐条添加到 mysql， 原因是redis中数据量很大，会出现高并发的对mysql数据库操作
            //这样的话，redis 存在的意义就没有了， 解决方案是  批量添加
            praiseList.add(praise);
        }


        //批量修改, update praise set praise_status = 0 where review_id in(....)
        if(praiseList.size()>0) {
            System.out.println("praiseList:" + praiseList);
            updatePraiseList(praiseList);
        }

        // 从redis 中删除
        if(redisTemplate.hasKey("cancle_praise_record")) {
            boolean result = redisTemplate.delete("cancle_praise_record");
            System.out.println("从redis中移除 customer_liked_record 的结果是：" + result);
        }


        //2. 获得redis 中 评论点赞总数，修改到mysql中
        Map<String,Integer> reviewPraiseCountMap =redisTemplate.opsForHash().entries("review_praise_count");
        System.out.println(reviewPraiseCountMap); //50035=4,

        Set<Map.Entry<String,Integer>> set2 = reviewPraiseCountMap.entrySet();
        for(Map.Entry<String,Integer> entry:set2) { //
            System.out.println("entry.getKey():"+entry.getKey());
            System.out.println("entry.getValue():"+entry.getValue());
            Integer reviewId =   Integer.valueOf(entry.getKey()  ); //50035
            Integer praiseCount =   entry.getValue() ;   // 4
            System.out.println("reviewId:" + reviewId);
            System.out.println("praiseCount:" + praiseCount);

            ShopReview review = new ShopReview();
            review.setShopReviewId(reviewId);
            review.setReviewPraise(praiseCount);

            reviewService.update(review);

        }


    }





    //批量添加
    public void saveAll(List<Praise> praiseList){
         praiseMapper.insertBatch(praiseList);
    }


    public void updatePraiseList(List<Praise> praiseList){
        praiseMapper.updateBatch(praiseList);
    }

}
